# -*-coding:utf8

import datetime
import os
import glob
import unittest
from BeautifulReport import BeautifulReport
from public.common.globalconfig import *
from public.common.mail import SendMail

# 刪除/run_all/img 文件夹下所有截图
for i in glob.glob(os.path.join(imgPath, '*.png')):
    os.remove(i)


def add_case(pattern='test*.py'):
    # unittest用例集合运行方法   testcase_patn   用例文件存放路径   'test_smoke_wp.py'   用例文件名称
    discover = unittest.defaultTestLoader.discover(
        start_dir=testcase_patn, pattern=pattern)
    return discover


def run_case(all_case):
    # 自动测试报告输出定义 description 用例名称   filename报告保存名称   log_path报告存放路径
    now = datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S')
    filename = '用例测试报告' + str(now)
    result = BeautifulReport(all_case)
    result.report(
        description='常规用例测试',
        filename=filename,
        log_path=report_path)
    return filename
    # sendemail(filename)


def sendemail(filename):
    # 邮件发送测试报告
    a = os.path.join(report_path, filename + '.html')
    b = SendMail(send_msg=a, attachment=a, receiver='obie.qy@gmail.com')
    b.send_mail()


if __name__ == '__main__':
    cases = add_case(pattern='test_baidu.py')
    for i in cases:
        if str(i) != '<unittest.suite.TestSuite tests=[]>':
            run_case(i)
            sendemail()